package com.xing.serviceedu.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.x.protobuf.MysqlxCursor;
import com.xing.commonutils.JwtUtils;
import com.xing.commonutils.R;
import com.xing.commonutils.user.UcenterMemberOrder;
import com.xing.serviceedu.client.UcenterClient;
import com.xing.serviceedu.entity.EduComment;
import com.xing.serviceedu.service.EduCommentService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/eduservice/comment")
@ApiModel("评论前端控制层")
public class CommentFrontController {

    @Autowired
    private EduCommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;

    //根据课程id查询评论列表
    @ApiOperation(value = "评论分页列表")
    @GetMapping("{page}/{limit}")
    public R index(@PathVariable Long page, @PathVariable long limit, String courseId) {
        //创建分页对象，传入当前页和页数参数
        Page<EduComment> pageParam = new Page<>(page, limit);
        //封装查询方法，把课程id参数传入
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        //课程业务层调用分页插件，把分页的参数和封装的数据传入
        commentService.page(pageParam, wrapper);

        //总记录数 --评论总条数
        List<EduComment> commentList = pageParam.getRecords();

        Map<String, Object> map = new HashMap<>();
        //总记录数
        map.put("items", commentList);
        //当前页
        map.put("Current", pageParam.getCurrent());
        //多少页
        map.put("Pages", pageParam.getPages());
        //大小
        map.put("Size", pageParam.getSize());
        //总计
        map.put("Total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("auth/save")
    public R save(@RequestBody EduComment comment, HttpServletRequest request) {

        //自己造了一个token来完成这个功能
        String jwtToken = JwtUtils.getJwtToken("1", "小三123");
        System.out.println("token:======="+jwtToken);
        //用来postman测试用的


        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        comment.setMemberId(memberId);
        UcenterMemberOrder userInfo = ucenterClient.getUserInfoOrder(memberId);
        comment.setNickname(userInfo.getNickname());
        comment.setAvatar(userInfo.getAvatar());
        commentService.save(comment);
        return R.ok();
    }


}
